-
Notifications
You must be signed in to change notification settings - Fork 3k
Avoid wait_ms() spin #5216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid wait_ms() spin #5216
Conversation
System's wait_ms() spins to achieve a precise delay - we don't want this. Call Thread::wait directly.
FAO @artokin |
See please and chime in there: #5194 (comment) . We shall create an issue for this? |
Looks like you're thinking along the same lines as me, but for different reasons. Yes, I guess this should be an issue. I agree with your proposal in that comment - Not sure what to do with wait() - threshold based on value being less than 0.001? I've seen people doing wait(10e-6). |
The reason for this change is that a blocking UARTSerial operation wants to poll quite frequently - here every millisecond, but because of the wait_ms(1) implementation, we'll end up spinning half the time on average. And worse, we could settle in to mostly-spinning or mostly-sleeping for prolonged periods, leading to noticeably different behaviour depending on which phase of the clock you first call the block. |
(And yes, obviously it should be using a proper wake-up mechanism rather than once-a-millisecond polling, but the first draft at such a mechanism got tangled up in review, and the ultimate solution which also handles |
I agree having two different wait behaviors and 4 different wait calls is a bit confusing. Especially that We can't do much about existing API right now, because of compatibility issues. But I've noticed that:
Is implemented incorrectly as
The API doc says |
Making it just call
|
Agreed, implementing |
you could avoid breaking some of the backwards compatibility by delaying for an extra ms - As for this PR, the change looks good to me. That way UARTSerial won't be blocked on the wait_ms discussion. |
Unfortunately adding one could cause significant problems the other way - people trying to construct a 10ms periodic thing with a 10ms wait would get significant periodic drift (they'd have some anyway, but this would add a lot). Expecting them to specify "9" to get a 10ms period would be counterintuitive. See this discussion here for people tearing their hair out over a built-in add 1 to RTX's osDelay that was quickly reverted. This scenario isn't quite the same, but I think the discussion is relevant. http://www.keil.com/forum/60393/ Not adding one is probably what the majority of users want - they'd get the period they expect with repeated calls. But there will be people wanting a minimum delay for some HW tolerance requirement... I'd say "change to ticks + 1" is my least favourite of the 3 options for
|
I don't think we can do much right now, except maybe implementing the functions in terms of the tickers. Going forward making both wait APIs work in the same way when they have the same name and having separate higher res (spining) wait function would make sense. |
Going back to the subject of the PR, there's also And it really is spinning with just a yield - to make it perform the same as the block, I'll change that to a 1 millisecond wait too. |
So far @c1728p9 reviewed, and me (LGTM). Anyone else ? We are having jenkins CI issues currently that will be fixed ,same for circle CI. Then we can trigger morph build. |
@kjbracey-arm Could you change the sha of the last commit to kick out circle ci? |
Spinning while polling is overly CPU intensive, and inconsistent with the current blocking behaviour of UARTSerial. Change to use Thread::wait(1) to match UARTSerial.
f45ec88
to
de4ced3
Compare
/morph test-nightly |
Result: SUCCESSYour command has finished executing! Here's what you wrote!
OutputAll builds and test passed! |
Build : FAILUREBuild number : 34 |
/morph build |
Build : SUCCESSBuild number : 103 Triggering tests/test mbed-os |
Test : SUCCESSBuild number : 49 |
System's wait_ms() spins to achieve a precise delay - we don't want this.
Call Thread::wait directly.
This PR is partly for testing, and to initiate discussion. I would personally like to see wait_ms() changed, or an alternative call in mbed_wait_api() brought in to avoid the unwanted CPU-sapping spin.